
Suppose that DS_t is the type of some data structure.

Here are six patterns for functions that use a DS_t
data structure as either an input or an output value.

The first three are used all the time.
The fourth is rarely used.
The last two are why C++ has copy constructors.

DS_t * f(...);                 // callee allocates DS_t, caller may or may not de-allocate

       f(const DS_t * x, ...); // caller allocates DS_t. DS_t is strictly an input

       f(DS_t * x, ...);       // caller allocates DS_t. DS_t may be input and/or output.

       f(DS_t ** x, ...);      // probably callee allocates DS_t and caller de-allocates it

DS_t   f(...);                 // DS_t automatically allocated in caller's local space

       f(DS_t x, ...);         // DS_t automatically allocated in callee's local space



Here are some questions that you should consider.


For each pattern above, find an example of a function
from the C or Windows library that uses that pattern.


The second pattern above is
      f(const DS_t * x, ...);
How is the meaning of this declaration changed by using
the keyword const in the following ways?
      f(DS_t const * x);
      f(DS_t * const x);
      f(const DS_t * const x);
      f(DS_t const * const x);


The fourth pattern above is
      f(DS_t ** x, ...);
Its comment uses the word "probably". Consider the
standard function declaration for main().
  int main(int argc, char ** argv)
This is NOT an example of the fourth pattern. Explain why.


Notice that the declaration
      f(const DS_t ** x, ...);
is equivalent to
      f(const DS_t * x[], ...);
but the second declaration implies a completely different
kind of pattern of use. What does the second declaration imply?
(Notice that what a declaration "implies" need not be what the code
that implements the declaration does. Unfortunately, the C language
is not as precise as it should be and what a declaration seems to
declare need not always be what the code that implements the
declaration does.)


How would you declare, and implement, a function createRandomArray()
that takes as input a positive integer n and returns an array of
integers whose size is a random number choosen between 1 and n?
Be sure the caller is informed of the actual size of the returned array.


How would you declare, and implement, a function createRandomSquareMatrix()
that takes as input a positive integer n and returns a two-dimensional,
square array of integers whose size is a random number choosen between 1
and n? Be sure the caller is informed of the actual size of the returned
array. (Note: The returned array should be one on which you can use the
double index notation a[i][j], where i and j are integers between 0 and n-1.)
